package com.lex.widget.header;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.lex.jfb.R;


/**
 * Created by Administrator on 2016/6/1.
 */
public abstract class BaseHeaderView extends LinearLayout{
    protected Context context;
    private LinearLayout headerLeft,headerCenter,headerRight;
    private boolean isShowLeft,isShowCenter,isShowRight;

    public BaseHeaderView(Context context){
        this(context,null);
    }

    public BaseHeaderView(Context context,AttributeSet attrs){
        this(context,attrs,0);
    }

    public BaseHeaderView(Context context,AttributeSet attrs,int defStyleAttr){
        super(context,attrs,defStyleAttr);
        this.context = context;
        getAttrs(attrs);
        findView();
        initView();
    }

    protected void getAttrs(AttributeSet attrs){
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.BaseHeaderView);
        isShowLeft = ta.getBoolean(R.styleable.BaseHeaderView_showLeft,true);
        isShowCenter = ta.getBoolean(R.styleable.BaseHeaderView_showCenter,true);
        isShowRight = ta.getBoolean(R.styleable.BaseHeaderView_showRight,true);
        ta.recycle();//回收
    }

    protected void findView(){
        View headerView = View.inflate(context,R.layout.widget_header_view,this);
        headerLeft = (LinearLayout) headerView.findViewById(R.id.header_left);
        headerCenter = (LinearLayout) headerView.findViewById(R.id.header_center);
        headerRight = (LinearLayout) headerView.findViewById(R.id.header_right);
    }

    public abstract View getLeftView();
    public abstract View getCenterView();
    public abstract View getRightView();


    protected void initView(){
        headerLeft.setVisibility(isShowLeft?VISIBLE:INVISIBLE);
        headerCenter.setVisibility(isShowCenter?VISIBLE:INVISIBLE);
        headerRight.setVisibility(isShowRight?VISIBLE:INVISIBLE);

        if (getLeftView()!=null)headerLeft.addView(getLeftView());
        if (getCenterView()!=null)headerCenter.addView(getCenterView());
        if (getRightView()!=null)headerRight.addView(getRightView());

        headerLeft.setOnClickListener(onClickListener);
        headerCenter.setOnClickListener(onClickListener);
        headerRight.setOnClickListener(onClickListener);
    }

    private OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.header_left_layout:
                    if (onHeaderClickListener != null){
                        if (context instanceof Activity){
                            ((Activity)context).finish();
                        }
                    }else onHeaderClickListener.onLeftClicked();
                    break;
                case R.id.header_center_layout:
                    if (onHeaderClickListener != null) onHeaderClickListener.onCenterClicked();
                    break;
                case R.id.header_right_layout:
                    if (onHeaderClickListener != null) onHeaderClickListener.onRightClicked();
                    break;
                default:
                    break;
            }
        }
    };
    private OnHeaderClickListener onHeaderClickListener;
    public void setOnHeaderClickListener(OnHeaderClickListener onHeaderClickListener){
        this.onHeaderClickListener = onHeaderClickListener;
    }

    public interface OnHeaderClickListener{
        void onLeftClicked();
        void onCenterClicked();
        void onRightClicked();
    }

}

